package wqh.xiao_kong.cfs2000;

import cn.tpson.dti.core.CFSServer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.sql.DriverManager;

/**
 * Created by devts on 2016/12/11.
 */
@WebServlet(name = "CFS2000Servlet")
public class CFS2000Servlet extends HttpServlet {


    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        //spring 管理，便于操作数据库
        ApplicationContext ac = new FileSystemXmlApplicationContext("classpath:/spring/spring-data.xml","classpath:/spring/spring-beans.xml");
        CFS2000Data cfs2000Data = (CFS2000Data)ac.getBean("cfs2000Data");
        //启动协议解析程序
        int port = Integer.parseInt(config.getInitParameter("port"));
        CFSServer.getInstance().setPort(port);
        CFSServer.getInstance().setProcessor(cfs2000Data);
        CFSServer.getInstance().start();
        System.out.println("消控协议解析启动#############################################");
    }

    @Override
    public void destroy() {
        super.destroy();
        CFSServer.getInstance().stop();

        try {
            DriverManager.deregisterDriver(DriverManager.getDrivers().nextElement());
        }catch (Exception e){
            e.printStackTrace();
        }

    }
}
